class SolutionZsh {
    public int minPathSum(int[][] grid) {

        int[][] result = new int[grid.length][grid[0].length];
        return pathSum(0, 0, grid, result);
    }

    public int pathSum(int x, int y, int[][] grid, int[][] result) {

        if (x == grid.length || y == grid[0].length) return 100_000;
        if (x == grid.length - 1 && y == grid[0].length - 1) return grid[x][y];
        if (result[x][y] == 0)
            result[x][y] = grid[x][y] + Math.min(pathSum(x + 1, y, grid, result), pathSum(x, y + 1, grid, result));
        return result[x][y];
    }
}